import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count:0,
    user_role:'normal'
  },
  mutations: {
    changeRole(state,role){
      state.user_role= role;
    },
    ADD(state){
      state.count = state.count + 1;
    },
    REDUCE(state){
      state.count = state.count - 1;
    }
  },
  getters:{
    evenOrOdd(state){
      return state.count % 2 === 0? '偶数':'奇数'
    }
  },
  actions: {
    addIfOdd({commit,state}){
      if(state.count % 2 === 1){ commit('ADD')}
    },
    addAsnc({commit}){
      const timer = setInterval(()=>{
        commit('ADD')
        clearInterval(timer)
      },2000)
    }
  }
})
